package com.tianhui.consignortwofei.mvp.presenter;

import android.text.TextUtils;

import com.fgs.common.CommonResponse;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.constant.PathType;
import com.tianhui.consignortwofei.mvp.contract.GoodsContract;
import com.tianhui.consignortwofei.mvp.model.GoodsModel;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.DispatcherInfo;
import com.tianhui.consignortwofei.mvp.model.enty.GoodsInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SourcePathInfo;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleLimitInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public class GoodsPresenter extends GoodsContract.Presenter {
    private GoodsModel mModel;

    public GoodsPresenter() {
        mModel = new GoodsModel();
    }

    @Override
    public void requestGoodsList(Map<String, String> map, boolean isDialog) {
//        mModel.goodsListModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<BaseResponse<GoodsDataList>>() {
//            @Override
//            public void onNext(BaseResponse<GoodsDataList> baseResponse) {
//                if (getView() != null) {
//                    if (baseResponse.getCode() == 20000) {
//                        GoodsDataList goodsData = baseResponse.getData();
//                        getView().setPageInfo(goodsData.current, goodsData.pages);
//                        List<GoodsInfo> goodsInfos = goodsData.records;
//                        if (null != goodsInfos && goodsInfos.size() > 0) {
//                            getView().showDataList(goodsInfos);
//                        } else {
//                            getView().showEmptyView();
//                        }
//                    } else {
//                        getView().setMsg(baseResponse.getMsg());
//                        getView().stopRefresh();
//                    }
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//
//                if (null != getView()) {
//                    getView().setMsg(e.message);
//                    getView().stopRefresh();
//                }
//            }
//        });
    }

    @Override
    public void requestDispatcherList(Map<String, String> map, boolean isDialog) {
        mModel.dispatcherListModel(getView().getContext(), isDialog, map, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DispatcherInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<DispatcherInfo>> commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        List<DispatcherInfo> list = commonResponse.getData();
                        if (list!= null&& list.size() > 0){
                            getView().showDispatcherList(list);
                        }

                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (getView() != null) {
                    getView().setMsg(e.message);
                }
            }
        });
    }

    @Override
    public void requestPublishLiteSource(GoodsInfo info) {
        mModel.publishLiteSourceModel(getView().getContext(), info, true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        getView().publicGoodsSuccess();
                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                getView().setMsg(e.message);
            }
        });
    }

    @Override
    public void requestPublishLiteSourceBuKai(GoodsInfo info) {
        mModel.publishLiteSourceModelBukai(getView().getContext(), info, true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        getView().publicGoodsSuccess();
                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                getView().setMsg(e.message);
            }
        });
    }


    private void requestSourcePathSave(SourcePathInfo pathInfo, int pathType) {
        Map<String, String> map = new HashMap<>();
        map.put("placeprovince", pathInfo.placeprovince);
        map.put("placecity", pathInfo.placecity);
        map.put("placecounty", pathInfo.placecounty);
        map.put("placedetails", pathInfo.placedetails);
        map.put("placetel", pathInfo.placetel);
        map.put("placeusername", pathInfo.placeusername);
        map.put("type", pathType + "");
        map.put("placecitycode", pathInfo.placecitycode);

        mModel.saveSourcePathInfoModel(getView().getContext(), map, true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        getView().setMsg("保存成功");
                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (getView() != null) {
                    getView().setMsg(e.message);
                }
            }
        });

    }

    @Override
    public void requestPlacePathSave(GoodsInfo goodsInfo) {
        if (TextUtils.isEmpty(goodsInfo.deliverprovince)) {
            getView().setMsg("请选择取单地");
            return;
        }
        if (TextUtils.isEmpty(goodsInfo.deliverusername)) {
            getView().setMsg("请填写取单人");
            return;
        }
        if (TextUtils.isEmpty(goodsInfo.delivertel)) {
            getView().setMsg("请填写取单人联系电话");
            return;
        }

        SourcePathInfo sourcePathInfo = new SourcePathInfo();
        sourcePathInfo.placeprovince = goodsInfo.placeprovince;
        sourcePathInfo.placecity = goodsInfo.placecity;
        sourcePathInfo.placecounty = goodsInfo.placecounty;
        sourcePathInfo.placeusername = goodsInfo.placeusername;
        sourcePathInfo.placetel = goodsInfo.placetel;
        sourcePathInfo.placedetails = goodsInfo.placedetails;
        sourcePathInfo.placecitycode = goodsInfo.placecitycode;
        requestSourcePathSave(sourcePathInfo, PathType.PLACE);
    }

    @Override
    public void requestDeliverPathSave(GoodsInfo goodsInfo) {
        if (TextUtils.isEmpty(goodsInfo.deliverprovince)) {
            getView().setMsg("请选择装货地");
            return;
        }
        if (TextUtils.isEmpty(goodsInfo.deliverusername)) {
            getView().setMsg("请填写装货人");
            return;
        }
        if (TextUtils.isEmpty(goodsInfo.delivertel)) {
            getView().setMsg("请填写装货人联系电话");
            return;
        }

        SourcePathInfo sourcePathInfo = new SourcePathInfo();
        sourcePathInfo.placeprovince = goodsInfo.deliverprovince;
        sourcePathInfo.placecity = goodsInfo.delivercity;
        sourcePathInfo.placecounty = goodsInfo.delivercounty;
        sourcePathInfo.placeusername = goodsInfo.deliverusername;
        sourcePathInfo.placetel = goodsInfo.delivertel;
        sourcePathInfo.placedetails = goodsInfo.deliverdetails;
        sourcePathInfo.placecitycode = goodsInfo.delivercitycode;
        requestSourcePathSave(sourcePathInfo, PathType.DELIVER);
    }

    @Override
    public void requestDestinationPathSave(GoodsInfo goodsInfo) {
        if (TextUtils.isEmpty(goodsInfo.destinationprovince)) {
            getView().setMsg("请选择目的地");
            return;
        }
        if (TextUtils.isEmpty(goodsInfo.destinationusername)) {
            getView().setMsg("请填写收货人");
            return;
        }
        if (TextUtils.isEmpty(goodsInfo.destinationtel)) {
            getView().setMsg("请填写收货人联系电话");
            return;
        }

        SourcePathInfo sourcePathInfo = new SourcePathInfo();
        sourcePathInfo.placeprovince = goodsInfo.destinationprovince;
        sourcePathInfo.placecity = goodsInfo.destinationcity;
        sourcePathInfo.placecounty = goodsInfo.destinationcounty;
        sourcePathInfo.placeusername = goodsInfo.destinationusername;
        sourcePathInfo.placetel = goodsInfo.destinationtel;
        sourcePathInfo.placedetails = goodsInfo.destinationdetails;
        sourcePathInfo.placecitycode = goodsInfo.destinationcitycode;
        requestSourcePathSave(sourcePathInfo, PathType.DESTINATION);
    }

    @Override
    public void requestGoodsType() {
        Map<String, String> map = new HashMap<>();
        map.put("pid", "73");
        mModel.dictionaryModel(getView().getContext(), map, true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DictionaryInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<DictionaryInfo>> commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        List<DictionaryInfo> dictionaryInfoList = commonResponse.getData();
                        if (dictionaryInfoList != null && dictionaryInfoList.size() > 0) {
                            getView().showGoodsTypeList(dictionaryInfoList);
                        }
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                getView().setMsg(e.message);
            }
        });
    }

    @Override
    public void requestCargoRights() {
        Map<String, String> map = new HashMap<>();
        map.put("pid", "2795");
        mModel.dictionaryModel(getView().getContext(), map, true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DictionaryInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<DictionaryInfo>> commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        List<DictionaryInfo> dictionaryInfoList = commonResponse.getData();
                        if (dictionaryInfoList != null && dictionaryInfoList.size() > 0) {
                            getView().showCargoRightsList(dictionaryInfoList);
                        }
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                getView().setMsg(e.message);
            }
        });
    }

    @Override
    public void requestVehicleLimit() {
        mModel.vehicleLimitListModel(getView().getContext(), true, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<List<VehicleLimitInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<VehicleLimitInfo>> commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        List<VehicleLimitInfo> vehicleLimitInfoList = commonResponse.getData();
                        if (vehicleLimitInfoList != null && vehicleLimitInfoList.size() > 0) {
                            VehicleLimitInfo vehicleLimitInfo = new VehicleLimitInfo();
                            vehicleLimitInfoList.add(0, vehicleLimitInfo);
                            getView().showVehicleLimitList(vehicleLimitInfoList);
                        }
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                getView().setMsg(e.message);
            }
        });
    }


}
